package LeetCode.month02.day38;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


public class No3446 {
    public static void main(String[] args) {
        int[][] arr = new int[][]{{1, 7, 3}, {9,8,2},{4,5,6}};
        for (int[] ints : arr) {
            System.out.println(Arrays.toString(ints));
        }
        System.out.println("======================");
        arr = sortMatrix(arr);
        for (int[] ints : arr) {
            System.out.println(Arrays.toString(ints));
        }
    }
    public static int[][] sortMatrix(int[][] grid) {
        int n = grid.length;
        for (int row = 0; row < n; row++) {
            int col = 0;
            for (int i = 0; i < n - row; i++) {
                int index1 = row+i;
                int index2 = col+i;
                for (int j = i+1; row+j < n; j++) {
                    if(grid[row+j][col+j]>grid[index1][index2]) {
                        index1 = row + j;
                        index2 = col + j;
                    }
                }
                if(index1!=row+i){
                    int temp = grid[row+i][col+i];
                    grid[row+i][col+i] = grid[index1][index2];
                    grid[index1][index2] = temp;
                }
            }
        }
        for (int col = 1; col < n; col++) {
            int row = 0;
            for (int i = 0; i < n - col; i++) {
                int index1 = row+i;
                int index2 = col+i;
                for (int j = i+1; col+j < n; j++) {
                    if(grid[row+j][col+j]<grid[index1][index2]) {
                        index1 = row + j;
                        index2 = col + j;
                    }
                }
                if(index1!=row+i){
                    int temp = grid[row+i][col+i];
                    grid[row+i][col+i] = grid[index1][index2];
                    grid[index1][index2] = temp;
                }
            }
        }
        return grid;
    }
}
